package com.neu.carbon.mes.controller;

import java.util.List;

import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerOutput;
import com.neu.carbon.mes.domain.*;
import com.neu.carbon.mes.service.*;
import com.neu.carbon.scm.service.IScmSaleOrderService;
import com.neu.carbon.wms.domain.WmsMaterialInfo;
import com.neu.carbon.wms.service.IWmsMaterialInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicResponseParameters;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.neu.common.annotation.Log;
import com.neu.common.core.controller.BaseController;
import com.neu.common.core.domain.AjaxResult;
import com.neu.common.enums.BusinessType;
import com.neu.common.utils.poi.ExcelUtil;
import com.neu.common.core.page.TableDataInfo;
//import sun.print.DialogOnTop;

import javax.annotation.Resource;

/**
 * 领料申请Controller
 * 
 * @author neuedu
 * @date 2024-08-13
 */
@Api(tags = {"领料申请"})
@RestController
@RequestMapping("/mesProduct/requisitionApply")
public class MesMaterialRequisitionController extends BaseController
{
    @Resource
    private IMesMaterialRequisitionService mesMaterialRequisitionService;
    @Resource
    private IWmsMaterialInfoService iWmsMaterialInfoService;

    @Resource
    private IMesProductPlanService iMesProductPlanService;
    @Resource
    private IMesProductScheduleService iMesProductScheduleService;
    @Resource
    private IMesMaterialRequisitionDetailService iMesMaterialRequisitionDetailService;
    @Resource
    private IMesProductModelService iMesProductModelService;

    /**
     * 查询领料申请列表
     */
    @GetMapping("/list")
    @ApiOperation("查询领料申请列表")
    @DynamicResponseParameters(properties = {
	        @DynamicParameter(name = "total", value = "总记录数"),
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "rows", value = "返回业务数据（数组类型）", dataTypeClass = MesMaterialRequisition.class),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    public TableDataInfo list(MesMaterialRequisition mesMaterialRequisition)
    {
        startPage();
        List<MesMaterialRequisition> list = mesMaterialRequisitionService.selectMesMaterialRequisitionList(mesMaterialRequisition);
        list.forEach(model->{
            if (model.getApplyUser().equals("1")){
                model.setApplyUserName("管理员");
            }
            if (model.getAuditUser().equals("1")){
                model.setAuditUserName("管理员");
            }
            WmsMaterialInfo wmsMaterialInfo = iWmsMaterialInfoService.selectWmsMaterialInfoById(model.getProductId());
            if (wmsMaterialInfo!=null){
                model.setMaterialName(wmsMaterialInfo.getName());
                model.setModel(wmsMaterialInfo.getModel());
                model.setSpecification(wmsMaterialInfo.getSpecification());
                model.setUnit(wmsMaterialInfo.getUnit());
            }
            MesProductPlan mesProductPlan = iMesProductPlanService.selectMesProductPlanById(model.getPlanId());
            if (mesProductPlan!=null){
                model.setPlanName(mesProductPlan.getSerialNo());
            }
            MesProductSchedule mesProductSchedule = iMesProductScheduleService.selectMesProductScheduleById(model.getScheduleId());
            if(mesProductSchedule!=null){
                model.setScheduleName(mesProductSchedule.getSerialNo());
                model.setScheduleId(mesProductSchedule.getId());
                model.setRequireQuantity(mesProductSchedule.getRequireQuantity());
                model.setProductDate(mesProductSchedule.getProductDate());
            }

        });
        return getDataTable(list);
    }

    /**
     * 导出领料申请列表
     */
    @ApiOperation("导出领料申请列表")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "文件名")
    })
    @PreAuthorize("@ss.hasPermi('mesProduct:requisitionApply:export')")
    @Log(title = "领料申请", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MesMaterialRequisition mesMaterialRequisition)
    {
        List<MesMaterialRequisition> list = mesMaterialRequisitionService.selectMesMaterialRequisitionList(mesMaterialRequisition);
        ExcelUtil<MesMaterialRequisition> util = new ExcelUtil<MesMaterialRequisition>(MesMaterialRequisition.class);
        return util.exportExcel(list, "requisitionApply");
    }

    /**
     * 获取领料申请详细信息
     */
    @ApiOperation("获取领料申请详细信息")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "data", value = "返回业务数据", dataTypeClass = MesMaterialRequisition.class),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        MesMaterialRequisition model = mesMaterialRequisitionService.selectMesMaterialRequisitionById(id);
        if (model.getApplyUser().equals("1")){
            model.setApplyUserName("管理员");
        }
        if (model.getAuditUser().equals("1")){
            model.setAuditUserName("管理员");
        }
        WmsMaterialInfo wmsMaterialInfo = iWmsMaterialInfoService.selectWmsMaterialInfoById(model.getProductId());
        if (wmsMaterialInfo!=null){
            model.setMaterialName(wmsMaterialInfo.getName());
            model.setModel(wmsMaterialInfo.getModel());
            model.setSpecification(wmsMaterialInfo.getSpecification());
            model.setUnit(wmsMaterialInfo.getUnit());

            List<MesMaterialRequisitionDetail> mesMaterialRequisitionDetailList = model.getMesMaterialRequisitionDetailList();
            mesMaterialRequisitionDetailList.forEach(item->{
                System.out.println(wmsMaterialInfo.getId());
                WmsMaterialInfo wmsMaterialInfo1 = iWmsMaterialInfoService.selectWmsMaterialInfoById(item.getMaterialId());

                if (item.getMaterialId().equals(wmsMaterialInfo1.getId())){
                    item.setMaterialName(wmsMaterialInfo1.getName());
                    item.setModel(wmsMaterialInfo1.getModel());
                    item.setSpecification(wmsMaterialInfo1.getSpecification());
                    item.setUnit(wmsMaterialInfo1.getUnit());
                    System.out.println(item);
                }
            });
        }


        MesProductPlan mesProductPlan = iMesProductPlanService.selectMesProductPlanById(model.getPlanId());
        if (mesProductPlan!=null){
            model.setPlanName(mesProductPlan.getSerialNo());
        }
        MesProductSchedule mesProductSchedule = iMesProductScheduleService.selectMesProductScheduleById(model.getScheduleId());
        if(mesProductSchedule!=null){
            model.setScheduleName(mesProductSchedule.getSerialNo());
            model.setScheduleId(mesProductSchedule.getId());
            model.setRequireQuantity(mesProductSchedule.getRequireQuantity());
            model.setProductDate(mesProductSchedule.getProductDate());

        }
        return AjaxResult.success(model);
    }

    /**
     * 新增领料申请
     */
    @ApiOperation("新增领料申请")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @PreAuthorize("@ss.hasPermi('mesProduct:requisitionApply:add')")
    @Log(title = "领料申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesMaterialRequisition mesMaterialRequisition)
    {
        return toAjax(mesMaterialRequisitionService.insertMesMaterialRequisition(mesMaterialRequisition));
    }

    /**
     * 修改领料申请
     */
    @ApiOperation("修改领料申请")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @PreAuthorize("@ss.hasPermi('mesProduct:requisitionApply:edit')")
    @Log(title = "领料申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesMaterialRequisition mesMaterialRequisition)
    {
        return toAjax(mesMaterialRequisitionService.updateMesMaterialRequisition(mesMaterialRequisition));
    }

    /**
     * 删除领料申请
     */
    @ApiOperation("删除领料申请")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @PreAuthorize("@ss.hasPermi('mesProduct:requisitionApply:remove')")
    @Log(title = "领料申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mesMaterialRequisitionService.deleteMesMaterialRequisitionByIds(ids));
    }
}
